﻿/**
 * ��ҳ��ת��
 * @author lvpw
 */
Milan.page = function()
{
}

/**
 * ��ʾ��¼���б?
 * @author lvpw
 */
Milan.page.showSizeList = function(element, evt)
{
	var div = Milan.page.findDivByClassName(element, "page-size-list");
	div.style.marginTop = (evt.clientY>document.body.offsetHeight/2?-1*div.offsetHeight-9:6)+"px";
	
	Milan.page.showSizeList.count++;
}
Milan.page.showSizeList.count = 0;

/**
 * ���ؼ�¼���б?
 * @author lvpw
 */
Milan.page.hideSizeList = function(element)
{
	Milan.page.showSizeList.count--;
	
	setTimeout(function(){
		if(Milan.page.showSizeList.count<1){
			if(!element.parentNode)
				return;
			Milan.page.findDivByClassName(element, "page-size-list").style.marginTop = "-9999px";
		}
	}, 500);
}

/**
 * �޸�ÿҳ��ʾ��¼��
 * @author lvpw
 */
Milan.page.size = function(element, pageSize, doAction)
{
	var form = Milan.page.form(element);

	if(form.elements["pageSize"].value==pageSize)
		return;
	
	form.elements["pageSize"].value = pageSize;
	
	Milan.page.to(element, 0, doAction);
}

/**
 * ��ʾ������תҳ�档
 * @author lvpw
 */
 Milan.page.showPageList = function(element, direction, evt)
 {
 	Milan.page.showPageList.scrollToFitPage(element);
 	Milan.page.showPageList.resetScroller(Milan.page.findDivByClassName(element, "scroller"));
 	
 	var div = Milan.page.findDivByClassName(element, "page-list");
 	var marginTop = evt.clientY>document.body.offsetHeight/2?-1*div.offsetHeight-9:6;
 	if(direction=="up")
 		marginTop = -1*div.offsetHeight-9;
 	else if(direction=="down")
 		marginTop = 6;
 	div.style.marginTop = marginTop+"px";
	
	Milan.page.showPageList.count++;
}
Milan.page.showPageList.count = 0;

/**
 * ���ؿ�����תҳ�档
 * @author lvpw
 */
Milan.page.hidePageList = function(element)
{
	Milan.page.showPageList.count--;
	
	setTimeout(function(){
		if(Milan.page.showPageList.count<1){
			if(!element.parentNode)
				return;
			Milan.page.findDivByClassName(element, "page-list").style.marginTop = "-9999px";
		}
	}, 500);
}

/**
 * ���Ϲ���ת��ҳ�롣
 * @author lvpw
 */
Milan.page.showPageList.up = function(element)
{
	if(element.className=="up-inactive" || Milan.page.showPageList.scrolling)
		return;
	
	Milan.page.showPageList.scrolling = true;
	
	var scroller = Milan.page.findDivByClassName(element, "scroller");
	var as = scroller.getElementsByTagName("a");
	Milan.page.showPageList.scroll(as[0], 0, 1);
}

/**
 * ���¹���ת��ҳ�롣
 * @author lvpw
 */
Milan.page.showPageList.down = function(element)
{
	if(element.className=="down-inactive" || Milan.page.showPageList.scrolling)
		return;
	
	Milan.page.showPageList.scrolling = true;
	
	var scroller = Milan.page.findDivByClassName(element, "scroller");
	var as = scroller.getElementsByTagName("a");
	Milan.page.showPageList.scroll(as[0], 0, -1);
}

/**
 * ���
 * @author lvpw
 */
Milan.page.showPageList.scroll = function(a, scrolled, direction)
{
	if(scrolled>=5*a.offsetHeight)
	{
		Milan.page.showPageList.scrolling = false;
		
		return;
	}
	
	if(scrolled%a.offsetHeight==0)
	{
		Milan.page.showPageList.resetScroller(a.parentNode);
		
		if((direction>0 && Milan.page.findDivByClassName(a.parentNode, "up-inactive")) || (direction<0 && Milan.page.findDivByClassName(a.parentNode, "down-inactive")))
		{
			Milan.page.showPageList.scrolling = false;
			
			return;
		}
	}
	
	a.style.marginTop = (Milan.page.toNumber(a.style.marginTop)+direction)+"px";
	
	setTimeout(function(){
		Milan.page.showPageList.scroll(a, scrolled+1, direction);
	}, 1);
}

Milan.page.showPageList.scrolling = false;

Milan.page.showPageList.scrollToFitPage = function(element)
{
	var form = Milan.page.form(element);
	
	if(form.elements["page-scroller-fit-page"].value==0)
		return;
	
	form.elements["page-scroller-fit-page"].value = 0;
	
	var pageNo = Milan.page.toNumber(form.elements["pageNumber"].value);
	
	if(pageNo<4)
		return;
	
	var scroller = Milan.page.findDivByClassName(element, "scroller");
	var as = scroller.getElementsByTagName("a");
	
	if(as.length<6)
		return;

	var marginTop = (3-pageNo)*as[0].offsetHeight;
	if(pageNo+2>as.length)
		marginTop += (pageNo+2-as.length)*as[0].offsetHeight;
	
	as[0].style.marginTop = marginTop+"px";
}

Milan.page.showPageList.resetScroller = function(scroller)
{
	var as = scroller.getElementsByTagName("a");
	if(as.length>5)
	{
		scroller.style.height = 5*as[0].offsetHeight+"px";
		
		var marginTop = Milan.page.toNumber(as[0].style.marginTop);
		
		if(marginTop<0 && Milan.page.findDivByClassName(scroller, "up-inactive"))
			Milan.page.findDivByClassName(scroller, "up-inactive").className = "up-active";
		
		if(marginTop>=0 && Milan.page.findDivByClassName(scroller, "up-active"))
			Milan.page.findDivByClassName(scroller, "up-active").className = "up-inactive";
		
		if(marginTop>(5-as.length)*as[0].offsetHeight && Milan.page.findDivByClassName(scroller, "down-inactive"))
			Milan.page.findDivByClassName(scroller, "down-inactive").className = "down-active";
		
		if(marginTop<=(5-as.length)*as[0].offsetHeight && Milan.page.findDivByClassName(scroller, "down-active"))
			Milan.page.findDivByClassName(scroller, "down-active").className = "down-inactive";
	}
}

Milan.page.toNumber = function(number)
{
    return number?Number(new String(number).match(/[-0-9]+/)):0;
}

Milan.page.findDivByClassName = function(element, className)
{
	var divs = element.parentNode.getElementsByTagName("div");
	for(var i = 0; i<divs.length; i++)
		if(divs[i].className==className)
			return divs[i];
	
	return null;
}

/**
 * ��ת��ָ��ҳ�档
 * @author lvpw
 */
Milan.page.to = function(element, pageNo, doAction)
{
	var form = Milan.page.form(element);
	
	if(form.elements["pageNumber"].value==pageNo)
		return;
	
	if(pageNo>0)
		form.elements["pageNumber"].value = pageNo;
	
	if(doAction!="")
		form.action = doAction;

	
	Milan.ajaxutility.formSubmit({
		form : form.id,
		success: function(response,option)
		{
			if(form.pagingCnt&&form.pagingCnt.value)
			{
				Milan.ajaxutility.updateContainer(form.pagingCnt.value,response.responseText);
				
				return;
			}
			Milan.pagejs.page_index.updateCurrentTab(response.responseText);
		}
	});
}

/**
 * ��ȡ��ǰ�ڵ�ĸ�form�ڵ㡣
 * @author lvpw
 */
Milan.page.form = function(element)
{
	var form = element;
	for(; form.tagName.toLowerCase()!="form"; form = form.parentNode);
	
	return form;
}

/**
 * updated by WinyWan on 2008-03-27
 * 
 * @param {Object} pageNo
 * @param {Object} pageSize
 * @param {Object} doAction
 */
Milan.goPage =function (ele,pageNo,pageSize,doAction)
{
	var form = ele.form;
	var objGoToPage = form.pageNo;
	var sGoToPage = objGoToPage.value.strip();
	
	if (sGoToPage == "")
	{
		sGoToPage = "1";
		objGoToPage.value = sGoToPage;
		
	}

	if (!Milan.string.isNumber(sGoToPage) || sGoToPage < 0)
	{
		sGoToPage = "1";
		objGoToPage.value = sGoToPage;
	}
	
	
	var objPageSize = form.pageSize;
	var vPageSize = objPageSize.value.strip();
	if (vPageSize == "")
	{
		vPageSize = "10"
		objPageSize.value = vPageSize;
	}
	
	
	if (!Milan.string.isNumber(vPageSize) || vPageSize <= 0)
	{
		vPageSize = "10"
		objPageSize.value = vPageSize;   
	}

	objGoToPage.value = sGoToPage;
	if (doAction!="") form.action = doAction;
	
	Milan.ajaxutility.formSubmit({
		form : form.id,
		success: function(response,option)
		{
			if(form.pagingCnt&&form.pagingCnt.value)
			{
				Milan.pagejs.page_index.updateContainer(form.pagingCnt.value,response.responseText);
				return;
			}
			Milan.pagejs.page_index.updateCurrentTab(response.responseText);
		}
	});
}

Milan.pageCtrlListener = function(e)
{
	var evnt = event||e ;
	evnt.cancelable = true;
	var tar = evnt.srcElement||evnt.target;
	if(evnt.keyCode != 13) return;
	if(tar.tagName.toUpperCase() == "INPUT" &&(tar.id.toUpperCase()=="PAGENO"||tar.id.toUpperCase()=="PAGESIZE"))
	{
		Milan.goPage(tar,"","","");
	}
}